package bean.managed;

import bean.session.DepartmentEntityFacade;
import bean.session.RawMaterialEntityFacade;
import bean.session.RawMaterialQuotationFacade;
import entity.VendorEntity;
import bean.session.VendorEntityFacade;

import entity.DepartmentEntity;
import entity.RawMaterialEntity;
import entity.RawMaterialQuotationEntity;
import entity.SystemUserEntity;
import entity.VendorProductPriceEntity;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import managedbean.AbstractBacking;
import org.primefaces.event.SelectEvent;
import type.RawMaterialQuotationType;
import util.Fixtures.Department;

@ManagedBean(name = "rawMaterialQuotationController")
@SessionScoped
public class RawMaterialQuotationController extends AbstractBacking implements Serializable {

    @ManagedProperty(value = "#{systemMessageController}")
    private SystemMessageController systemMessageController;

    public SystemMessageController getSystemMessageController() {
        return systemMessageController;
    }

    public void setSystemMessageController(SystemMessageController systemMessageController) {
        this.systemMessageController = systemMessageController;
    }
    private Collection<RawMaterialEntity> rawMaterial;

    public Collection<RawMaterialEntity> getRawMaterial() {
        return facade.findAllIncludeArchived();
    }

    public void setRawMaterial(Collection<RawMaterialEntity> rawMaterial) {
        this.rawMaterial = rawMaterial;
    }
    private RawMaterialEntity currRawMaterial;

    public RawMaterialEntity getCurrRawMaterial() {
        return currRawMaterial;
    }

    public Date getToday() {
        Date today = new Date();
        return today;
    }

    public void setCurrRawMaterial(RawMaterialEntity currRawMaterial) {
        this.currRawMaterial = currRawMaterial;
    }
    private RawMaterialEntity selectedRawMaterial;

    public RawMaterialEntity getSelectedRawMaterial() {
        return selectedRawMaterial;
    }

    public void setSelectedRawMaterial(RawMaterialEntity selectedRawMaterial) {
        this.selectedRawMaterial = selectedRawMaterial;
    }

    public String rawMaterialSelect(SelectEvent event) {

        currRawMaterial = selectedRawMaterial;



        return "PurchaseRequisitionInfo?faces-redirect=true";
    }
    private Date validity;
    private double quantity;
    //////////////////////////////////////////

    public double getQuantity() {
        return quantity;
    }

    public void setQuantity(double quantity) {
        this.quantity = quantity;
    }

    public Date getValidity() {
        return validity;
    }

    public void setValidity(Date validity) {
        this.validity = validity;
    }

    public String createRMQuotation() {
  if(getVppPurchaseList().size()==0||quantity==0){
            FacesContext context = FacesContext.getCurrentInstance();
            FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, "You have to enter some input!", "Check your input!");
            context.addMessage(null, message);
            return "";
        }
        for (VendorEntity v : getVppPurchaseList()) {

            //Collection<RawMaterialQuotationEntity> rmqs =new ArrayList<RawMaterialQuotationEntity>();
            RawMaterialQuotationEntity rmq = new RawMaterialQuotationEntity();
            rmq.setQuantity(quantity);
            rmq.setValidity(validity);
            rmq.setVendor(v);
            rmq.setType(RawMaterialQuotationType.notquoted);
            rmq.setRawMaterial(selectedRawMaterial);
            //  rmq.setSystemUser(getCurrentUser());

            //rmqs.add(rmq);
            //  getCurrentUser().get
            v.getRawMaterialQuotation().add(rmq);

            getEjbFacade().edit(v);
//
////begin timer, quotation will expire when validity period is due.
//            if (rmq.getValidity() != null) {
//                getRqFacade().scheduleTimer(rmq.getValidity(), rmq.getId());
//            }

        }
        vppPurchaseList.clear();
        validity = null;
        quantity = 0;
        FacesContext context = FacesContext.getCurrentInstance();
        context.addMessage(null, new FacesMessage("Quotation Created", "Quotation Sent to Supplier"));
        return "main?faces-redirect=true";
    }
    //private List<RawMaterialEntity> allRMEntityList;
    //private List<RawMaterialEntity> userRightsEntityList;
    //this quotation has the raw material ,true ,otherwise false

    public boolean checkVendorProduct(VendorProductPriceEntity vpp) {

        if (vpp.getRawMaterial().getProductName().equalsIgnoreCase(currRawMaterial.getProductName())) {
            return true;
        }


        return false;
    }
    private Collection<VendorEntity> vendors;

    public void setVendors(Collection<VendorEntity> vendors) {
        this.vendors = vendors;
    }

    public Collection<VendorEntity> getVendors() {
        Collection<VendorEntity> result = new ArrayList<VendorEntity>();

        for (VendorEntity v : selectedRawMaterial.getVendor()) {
            if (!v.isLocked()) {
                result.add(v);
            }
        }
        return result;


    }
    private VendorEntity vendor;
    @EJB
    private bean.session.RawMaterialEntityFacade facade;
    @EJB
    private bean.session.VendorEntityFacade ejbFacade;
    @EJB
    private bean.session.RawMaterialQuotationFacade rqFacade;

    public RawMaterialQuotationFacade getRqFacade() {
        return rqFacade;
    }

    public void setRqFacade(RawMaterialQuotationFacade rqFacade) {
        this.rqFacade = rqFacade;
    }

    public RawMaterialEntityFacade facade() {
        return facade;
    }

    public void setFacade(RawMaterialEntityFacade facade) {
        this.facade = facade;
    }

    public VendorEntity getVendor() {
        if (getLoginVendor() != null) {
            return getLoginVendor();
        }
        return null;
    }

    public void setVendor(VendorEntity vendor) {
        this.vendor = vendor;
    }
    //vpp of all vendor in the system .
    private VendorEntity selectedVendor;
//////////////////////////////////////////  
    private VendorEntity selectedVpp;

    public VendorEntity getSelectedVpp() {
        return selectedVpp;
    }

    public void setSelectedVpp(VendorEntity selectedVpp) {
        this.selectedVpp = selectedVpp;
        //   vppPurchaseList.add(selectedVpp);
    }
    private List<VendorEntity> vppPurchaseList = new ArrayList<VendorEntity>();

    public List<VendorEntity> getVppPurchaseList() {
        return vppPurchaseList;
    }

    public void setVppPurchaseList(List<VendorEntity> vppPurchaseList) {
        this.vppPurchaseList = vppPurchaseList;
    }

    public VendorEntity getSelectedVendor() {
        return selectedVendor;
    }

    public void setSelectedVendor(VendorEntity selectedVendor) {
        this.selectedVendor = selectedVendor;
    }

    public VendorEntityFacade getEjbFacade() {
        return ejbFacade;
    }

    public void setEjbFacade(VendorEntityFacade ejbFacade) {
        this.ejbFacade = ejbFacade;
    }
    private Collection<RawMaterialQuotationEntity> rmqqs;

    public Collection<RawMaterialQuotationEntity> getRmqqs() {
        rmqqs = getRqFacade().getQuotedQuotation();
        return rmqqs;
    }

    public void setRmqqs(Collection<RawMaterialQuotationEntity> rmqqs) {
        this.rmqqs = rmqqs;
    }
    private Collection<RawMaterialQuotationEntity> rmqs;

    public Collection<RawMaterialQuotationEntity> getRmqs() {
        rmqs = getRqFacade().getUnquotedQuotation();
        return rmqs;
    }

    public void setRmqs(Collection<RawMaterialQuotationEntity> rmqs) {
        this.rmqs = rmqs;
    }

    public RawMaterialQuotationController() {
    }
    //for edit raw material quotation
    private RawMaterialQuotationEntity selectedRawMaterialQuotation;

    public RawMaterialQuotationEntity getSelectedRawMaterialQuotation() {
        return selectedRawMaterialQuotation;
    }

    public void setSelectedRawMaterialQuotation(RawMaterialQuotationEntity selectedRawMaterialQuotation) {
        this.selectedRawMaterialQuotation = selectedRawMaterialQuotation;
    }
    private RawMaterialQuotationEntity currRawMaterialQuotation;

    public RawMaterialQuotationEntity getCurrRawMaterialQuotation() {
        return currRawMaterialQuotation;
    }

    public void setCurrRawMaterialQuotation(RawMaterialQuotationEntity currRawMaterialQuotation) {
        this.currRawMaterialQuotation = currRawMaterialQuotation;
    }

    public String rawMaterialQuotationSelect(SelectEvent event) {

        currRawMaterialQuotation = selectedRawMaterialQuotation;



        return "EditQuotation?faces-redirect=true";
    }
    @EJB
    private bean.session.DepartmentEntityFacade departmentFacade;

    public DepartmentEntityFacade getDepartmentFacade() {
        return departmentFacade;
    }

    public void setDepartmentFacade(DepartmentEntityFacade departmentFacade) {
        this.departmentFacade = departmentFacade;
    }

    public String editRMQuotation() {
        currRawMaterialQuotation.setPrice(price);
        currRawMaterialQuotation.setNote(note);
        currRawMaterialQuotation.setCost(cost);
        currRawMaterialQuotation.setType(RawMaterialQuotationType.quoted);

//        String message = 
//          DepartmentEntity pur = departmentFacade.findDepartmentByName(Department.Purchasing.getName());
//            SystemUserEntity productionManager = pur.getManager();
//  systemMessageController.getEjbFacade().createAndSendSystemMessage(productionManager,
//                        message, "/entities/salesForecastEntity/List.xhtml", systemMessageController);
        getRqFacade().edit(currRawMaterialQuotation);


        if (getRqFacade().checkAllQuotationsSameCategoryHasBeenQuoted(currRawMaterialQuotation)) {
            String message = "All Quotations for " + currRawMaterialQuotation.getRawMaterial().getProductName() + " with the validity date at "
                    + currRawMaterialQuotation.getValidity() + " has been quoted.Ready to initiate this order ";
            DepartmentEntity pur = departmentFacade.findDepartmentByName(Department.Purchasing.getName());
            SystemUserEntity productionManager = pur.getManager();
            systemMessageController.getEjbFacade().createAndSendSystemMessage(productionManager,
                    message, "/entities/purchasing/PriceComparison.xhtml", systemMessageController);
        }
        System.out.println(currRawMaterialQuotation.getType() + "wwwwww");
        note = "";
        price = 0;
        cost = 0;
        FacesContext context = FacesContext.getCurrentInstance();
        context.addMessage(null, new FacesMessage("Quotation Updated", "Quotation Updated"));
        return "";
    }
    //unit price
    private double price;
    private String note;
    private double cost;

    public boolean checkAllQuotationsSameCategoryHasBeenQuoted(RawMaterialQuotationEntity r) {
        return false;
    }

    public double getCost() {
        return cost;
    }

    public void setCost(double cost) {
        this.cost = cost;
    }

    public String getNote() {
        return note;
    }

    public void setNote(String note) {
        this.note = note;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }
    private RawMaterialQuotationEntity selectedQuota;

    public RawMaterialQuotationEntity getSelectedQuota() {
        if (selectedQuota == null) {
            selectedQuota = new RawMaterialQuotationEntity();

        }

        return selectedQuota;
    }

    public void setSelectedQuota(RawMaterialQuotationEntity selectedQuota) {
        this.selectedQuota = selectedQuota;

    }

    public String delete() {

        getRqFacade().originalRemove(selectedQuota);
        rmqqs = null;
        FacesContext context = FacesContext.getCurrentInstance();
        context.addMessage(null, new FacesMessage("Quotation Deleted", "Quotation Deleted"));
        return "MaintainQuotation";
    }

    public String delete1() {

        getRqFacade().originalRemove(selectedQuota);
        rmqs = null;
        FacesContext context = FacesContext.getCurrentInstance();
        context.addMessage(null, new FacesMessage("Quotation Deleted", "Quotation Deleted"));
        return "MaintainQuotation";
    }
    private Date lastDate;

    public Date getLastDate() {
        return lastDate;
    }

    public void setLastDate(Date lastDate) {
        this.lastDate = lastDate;
    }

    public String invalidQuotation() {
        List<RawMaterialQuotationEntity> result = getRqFacade().getInvalidQuotation(getLastDate());
        for (RawMaterialQuotationEntity q : result) {
            getRqFacade().originalRemove(q);
        }

        FacesContext context = FacesContext.getCurrentInstance();
        context.addMessage(null, new FacesMessage("Quotations Deleted", "Quotations Deleted"));
        return "";
    }
}
